package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.*;
import com.javasm.service.UserService;
import com.javasm.service.impl.MenuServiceImpl;
import com.javasm.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @version 0.1
 * @className: UserServlet
 * @description:
 * @author: gfs
 * @date: 2025/6/23 10:52
 * @since: jdk17
 */
@WebServlet("/users/*")
public class UserServlet extends BaseServlet {

    protected void listUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        //1接参数 封装对象
        //页码参数
        Integer page = 1;
        Integer pageSize = 5;

        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        //如果传了 就用传来的值 没传使用默认值
        if(pageStr!=null&&!"".equals(pageStr)){
            page = Integer.valueOf(pageStr);
        }
        if(pageSizeStr!=null&&!"".equals(pageSizeStr)){
            pageSize = Integer.valueOf(pageSizeStr);
        }

        //查询条件
        String username = req.getParameter("username");
        String isvalidStr = req.getParameter("isvalid");

        Integer isvalid = null;
        if(isvalidStr!=null&&!"".equals(isvalidStr)){
            isvalid = Integer.valueOf(isvalidStr);
        }
        AdminUser inputUser = new AdminUser(username, isvalid);
        //2调用servcie

        UserService userService = new UserServiceImpl();
        Integer total = userService.countUser(inputUser);
        //处理异常情况 超最大页数
        Integer totalPage = PageInfo.getTotalPage(total, pageSize);
        if(page>1 && page>totalPage){
            page = totalPage;
        }
        List<AdminUser> listUser = userService.listUser(inputUser, page, pageSize);
        //3返回响应
        ReturnResult returnResult = new ReturnResult();
        if(listUser.size()>0){
            //把页码相关参数 都放在一起 方便前端覆盖
            //page pageSize total
            PageInfo pageInfo = new PageInfo(page, pageSize, total);
            returnResult.setCode(ReturnCodeEnum.QUERY_SUCCESS.getCode());
            returnResult.setMsg(ReturnCodeEnum.QUERY_SUCCESS.getMsg());
            returnResult.setPageInfo(pageInfo);
            returnResult.setReturnData(listUser);
        }else{
            returnResult.setCode(ReturnCodeEnum.QUERY_NODATA.getCode());
            returnResult.setMsg(ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();




    }

    protected void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        //1接收参数 封装对象
        String username = req.getParameter("username");
        String headImg = req.getParameter("headImg");

        HttpSession session = req.getSession();
        AdminUser loginUser =(AdminUser) session.getAttribute("loginUser");
        AdminUser inputUser = new AdminUser();
        inputUser.setUsername(username);
        inputUser.setHeadImg(headImg);
        //把当前登录(使用添加功能的用户编号传入) 作为创建人编号
        inputUser.setCreateUid(loginUser.getUid());

        //调用service
        UserService userService = new UserServiceImpl();
        Integer resNum = userService.insertUser(inputUser);

        //反馈结果
        ReturnResult returnResult = new ReturnResult();
        if(resNum>0){
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();




    }
    protected void updateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1接收参数 封装对象
        String username = req.getParameter("username");
        String headImg = req.getParameter("headImg");
        String password = req.getParameter("password");
        String isvalidStr = req.getParameter("isvalid");
        String uidStr = req.getParameter("uid");
        Integer isvalid = null;
        Long uid = null;
        if(isvalidStr!=null&&!"".equals(isvalidStr)){
            isvalid = Integer.valueOf(isvalidStr);
        }
        if(uidStr!=null&&!"".equals(uidStr)){
            uid = Long.valueOf(uidStr);
        }
        AdminUser inputUser = new AdminUser(uid, username, password, isvalid, headImg);

        //调用service
        UserService userService = new UserServiceImpl();
        Integer resNum = userService.updateUser(inputUser);

        //反馈结果
        ReturnResult returnResult = new ReturnResult();
        if(resNum>0){
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();

    }
    protected void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1接收参数 封装对象
        String listUidStr = req.getParameter("listUid");
        //删除列表
        List<Long> listUid = new ArrayList<>();
        for(String uidStr: listUidStr.split(",")){
            listUid.add(Long.valueOf(uidStr));
        }
        //调用service
        UserService userService = new UserServiceImpl();
        Integer resNum = userService.deleteUser(listUid);
        //反馈结果
        ReturnResult returnResult = new ReturnResult();
        if(resNum>0){
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();

    }

    protected void readyForAuth(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uidStr = req.getParameter("uid");
        Long uid = null;
        if(uidStr!=null&&!"".equals(uidStr)){
            uid = Long.valueOf(uidStr);
        }

        MenuServiceImpl menuService = new MenuServiceImpl();
        List<AdminMenu> listMenuWithSubMenu = menuService.ListAllMenuWithSubMenu();
        List<Long> userMenuId = menuService.ListUserMenuId(uid);

        HashMap<String, Object> returnMap = new HashMap<>();
        returnMap.put("menuWithSub",listMenuWithSubMenu);
        returnMap.put("userMenuId",userMenuId);

        //反馈结果
        ReturnResult returnResult = new ReturnResult();

        returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode());
        returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        returnResult.setReturnData(returnMap);

        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();




    }

    protected void updateUserAuth(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String uidStr = req.getParameter("uid");
        Long uid = null;
        if(uidStr!=null&&!"".equals(uidStr)){
            uid = Long.valueOf(uidStr);
        }
        String listMidStr = req.getParameter("listMid");
        ArrayList<Long> listMid = null;
        //字符串拆成数组 转成Long 加入listMid
        //如果传了值
        if(listMidStr!=null&&!"".equals(listMidStr)){
            listMid =  new ArrayList<>();
            for(String mid : listMidStr.split(",")){
                listMid.add(Long.valueOf(mid));
            }
        }
        //调用servcie
        UserService userService = new UserServiceImpl();
        Boolean flag = userService.updateUserAuth(listMid, uid);

        //反馈结果
        ReturnResult returnResult = new ReturnResult();
        if(flag){
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();



    }



    }
